import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;


public class CharacterSelect extends JFrame{

	Game game;
	MyPanel panel;
	JButton char0,char1,char2,char3,char4,char5;
	JTextField text1;
	JTextField text0;
	JLabel label0;
	JLabel label1;
	int int0,int1,int2,int3,int4,int5;
	JLabel title;
	boolean end;
	public CharacterSelect(Game game)
	{
		
		int0 = 0;
		int1 = 5;
		int2 = 8;
		int3 = 11;
		int4 = 20;
		int5 = 21;
		end = true;
		this.setVisible(true);
		this.game = game;
		this.panel = new MyPanel();
		title = new JLabel("Character Select Screen");
		char0 = new JButton();
		char1 = new JButton();
		char2 = new JButton();
		char3 = new JButton();
		char4 = new JButton();
		char5 = new JButton();
		text0 = new JTextField();
		label0 = new JLabel("IP Address:");
		label1 = new JLabel("Name: ");
		text0.setColumns(15);
		text1 = new JTextField();
		text1.setColumns(10);
		char0.setIcon(new ImageIcon(GameSettings.getImageStorage().getImage(int0, 0,0)));
		char1.setIcon(new ImageIcon(GameSettings.getImageStorage().getImage(int1, 0,0)));
		char2.setIcon(new ImageIcon(GameSettings.getImageStorage().getImage(int2, 0,0)));
		char3.setIcon(new ImageIcon(GameSettings.getImageStorage().getImage(int3, 0,0)));
		char4.setIcon(new ImageIcon(GameSettings.getImageStorage().getImage(int4, 0,0)));
		char5.setIcon(new ImageIcon(GameSettings.getImageStorage().getImage(int5, 0,0)));
		char0.addActionListener(new Selector(int0));            //Goku
		char1.addActionListener(new Selector(int1));            //Gohan
		char2.addActionListener(new Selector(int2));            //Vegeta
		char3.addActionListener(new Selector(int3));            //Trunks
		char4.addActionListener(new Selector(int4));            //Android 17
		char5.addActionListener(new Selector(int5));            //Android 18
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.CENTER;
		c.gridwidth = 8;
		panel.add(title,c);
		c.gridwidth = 1;
		c.gridy = 1;
		c.fill = GridBagConstraints.NONE;
		panel.add(label0,c);
		c.gridwidth = 2;
		panel.add(text0,c);
		c.gridwidth = 1;
		panel.add(label1,c);
		c.gridwidth = 1;
		panel.add(text1,c);
		c.gridwidth = 1;
		c.gridy = 2;
		panel.add(char0,c);
		panel.add(char1,c);
		panel.add(char2,c);
		panel.add(char3,c);
		panel.add(char4,c);
		panel.add(char5,c);
		this.add(panel);
		pack();
		this.addWindowListener( new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
            	try{
            	GameSettings.getDBComm().close();
            	}
            	catch(Exception e)
            	{
            		
            	}
            	if(end)
                System.exit(0);
            }
        } );
	}
	private void closeWindow()
	{
		WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
	}
	private class Selector implements ActionListener
	{
		int val;
		public Selector(int val)
		{
			this.val = val;
		}
		public void actionPerformed(ActionEvent arg0) {
			game.setCharacterNumber(val);
			end = false; 
			if(text1.getText().toString() == "" || text1.getText().toString() == null || text1.getText().toString().length() == 0)
			{
				GameSettings.setName("Anonymous");
			}
			else
			{
				GameSettings.setName(text1.getText().toString());
			}
			if(text0.getText().toString() != null && text0.getText().toString().length() >=5)
			{
				GameSettings.setIp(text0.getText().toString());
			}
			closeWindow();
			
		}
		
	}
	public class MyPanel extends JPanel
	{
		public MyPanel()
		{
			super(new GridBagLayout());
		}
	}
	
}
